<?php
	include('simple_html_dom.php');

	//ham get source cua imgs trong site voi duong link cho truoc
	function getImgInfo($urlSite){
		$scheme = parse_url($urlSite,0);
		$host = parse_url($urlSite,1);
	
		function dlPage($href) {

		    $curl = curl_init();
		    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		    curl_setopt($curl, CURLOPT_HEADER, false);
		    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
		    curl_setopt($curl, CURLOPT_URL, $href);
		    curl_setopt($curl, CURLOPT_REFERER, $href);
		    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
		    curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.125 Safari/533.4");
		    $str = curl_exec($curl);
		    curl_close($curl);

			// Create a DOM object
		    $dom = new simple_html_dom();
		    // Load HTML from a string
		    $dom->load($str);

		    return $dom;
		    }
		//tao Dom de xu ly tung element
		$html = dlPage($urlSite);

		//tim the title cua site
		$title = $html->find('title',0);
		$infoImgs[] = $title->innertext;
		

		//tim tat ca the img cua file html
		$imgEls = $html->find('img');

		//$max_count la bien dem tong phan tu cua $els
		$max_count = 0;
		foreach ($imgEls as $el) {
			$max_count++;
		}

		//kiem tra neu so phan tu lon hon 15 thi lay 15
		$max_count = $max_count <=30 ? $max_count : 30;

		$i=0 ; //bien dem de break khi vuot muc 30 vong lap

		//duyet qua tung phan tu trong the dom img
		foreach ($imgEls as $el) {
			//lay tat ca element img
			$imgSrc = $el->src;

			//kiem tra khong la file anh gif
			if(substr($imgSrc, -4) != '.gif') {

				//kiem tra thu src cua anh co chua host và http khong va tao duong link hoan chinh
				if(is_null(parse_url($imgSrc,0))){
					//kiem tra scheme co http khong
					if(substr($imgSrc,0,1)=='/'){
						$imgSrc = $scheme.'://'.$host.$imgSrc;
					} elseif (substr($imgSrc,0,1)=='.') {
						if (substr($imgSrc,1,1)=='.') {
							//neu src co dang ../img/
							$imgSrc = $scheme.'://'.$host.substr($imgSrc,2);
						} elseif (substr($imgSrc,1,1)=='/') {
							//neu src co dang ./img
							$imgSrc = $scheme.'://'.$host.substr($imgSrc,1);
						}
					}
				} //end if cua truong hop khong co http
				
				//kiem tra width, height cua image
				list($width,$height)  = getimagesize($imgSrc);
				if($width > 250 && $height > 250){
					//xuat ra the html img cua hinh get duoc
					$infoImgs[] = $imgSrc;
				}
			}
			$i++;
			if($i>$max_count){
				break;//vuot qua 30 vong thi thoat ra
			}
		}
		return $infoImgs;
	}
	
?>